/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kasgroup2;

/**
 *
 * @author beha
 */
public class UISpeakersOverview extends javax.swing.JPanel {

    private TableManager tmSpeakers = null;
    private TableManager tmSubjects = null;
    /**
     * Creates new form UISpeakersOverview
     */
    public UISpeakersOverview() {
        initComponents();
        tmSpeakers = new TableManager(this.tblSpeakers);
        tmSubjects = new TableManager(this.tblSubjects);
        fillSpeakerTable();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAddSpeaker = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblSubjects = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblSpeakers = new javax.swing.JTable();
        btnRefreshSpeakers = new javax.swing.JButton();
        btnDeleteSpeaker = new javax.swing.JButton();
        btnEditSpeaker = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Speakers Overview"));

        btnAddSpeaker.setText("Add");
        btnAddSpeaker.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddSpeakerActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Subjects for speaker"));

        tblSubjects.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null}
            },
            new String [] {
                "Name", "Description"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblSubjects.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane3.setViewportView(tblSubjects);
        tblSubjects.getColumnModel().getColumn(0).setResizable(false);
        tblSubjects.getColumnModel().getColumn(1).setResizable(false);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 535, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Speakers"));

        tblSpeakers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null}
            },
            new String [] {
                "Name"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblSpeakers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblSpeakers.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblSpeakersMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblSpeakers);
        tblSpeakers.getColumnModel().getColumn(0).setResizable(false);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
        );

        btnRefreshSpeakers.setText("Refresh");
        btnRefreshSpeakers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshSpeakersActionPerformed(evt);
            }
        });

        btnDeleteSpeaker.setText("Delete");
        btnDeleteSpeaker.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteSpeakerActionPerformed(evt);
            }
        });

        btnEditSpeaker.setText("Edit");
        btnEditSpeaker.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditSpeakerActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(btnRefreshSpeakers)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnAddSpeaker)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnEditSpeaker)
                        .add(7, 7, 7)
                        .add(btnDeleteSpeaker))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnRefreshSpeakers)
                    .add(btnAddSpeaker)
                    .add(btnDeleteSpeaker)
                    .add(btnEditSpeaker))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddSpeakerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddSpeakerActionPerformed
        UICreateSpeakerFrame uicsf = new UICreateSpeakerFrame();
        uicsf.setVisible(true);
        uicsf.pack();
        fillSpeakerTable();
    }//GEN-LAST:event_btnAddSpeakerActionPerformed

    private void tblSpeakersMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblSpeakersMouseClicked
        fillSubjectsTable();
    }//GEN-LAST:event_tblSpeakersMouseClicked

    private void btnRefreshSpeakersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshSpeakersActionPerformed
        fillSpeakerTable();
    }//GEN-LAST:event_btnRefreshSpeakersActionPerformed

    private void btnDeleteSpeakerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteSpeakerActionPerformed
        String selRowValue = (String)tmSpeakers.getCellValueForSelectedRow(0);
        KASGroup2.getCoreHandlerInstance().removeSpeaker(selRowValue);
        fillSpeakerTable();
        tmSubjects.clear();
    }//GEN-LAST:event_btnDeleteSpeakerActionPerformed

    private void btnEditSpeakerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditSpeakerActionPerformed
        Speaker selSpeaker = KASGroup2.getCoreHandlerInstance().getSpeaker(this.tmSpeakers.getSelectedRowIndex());
        if(selSpeaker != null) {
            UICreateSpeakerFrame uicsf = new UICreateSpeakerFrame(selSpeaker);
            uicsf.setVisible(true);
            uicsf.pack();
            fillSpeakerTable();
        }
    }//GEN-LAST:event_btnEditSpeakerActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddSpeaker;
    private javax.swing.JButton btnDeleteSpeaker;
    private javax.swing.JButton btnEditSpeaker;
    private javax.swing.JButton btnRefreshSpeakers;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tblSpeakers;
    private javax.swing.JTable tblSubjects;
    // End of variables declaration//GEN-END:variables

    private void fillSpeakerTable() {
        tmSpeakers.clear();
        for(Speaker s : KASGroup2.getCoreHandlerInstance().getSpeakerList()) {
            tmSpeakers.addRow(new Object[] { s.getName() });
        }
    }

    private void fillSubjectsTable() {
        String selSpeakerName = (String)tmSpeakers.getCellValueForSelectedRow(0);
        //System.out.println("Speaker name: " + selSpeakerName);
        tmSubjects.clear();
        for(Subject s : KASGroup2.getCoreHandlerInstance().getSpeakerSubjects(selSpeakerName)) {
            tmSubjects.addRow(new Object[] { s.getName(), s.getDescription() });
            //System.out.println("Subject: " + s.getName() + " - " + s.getDescription());
        }
    }
}
